<template>
  <div class="cus">
    <div class="cus_box">
      <div class="title">
        <span>Change Password</span>
        <i @click="$emit('changePwdCall')" class="el-icon-close"></i>
      </div>
      <div class="content">
        <el-form
          :model="dataForm"
          :rules="dataRule"
          ref="dataForm"
          label-width="auto"
        >
          <el-form-item label="New Password" prop="password">
            <el-input
              v-model="dataForm.password"
              type="text"
              placeholder="Please Enter"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="password2">
            <el-input
              v-model="dataForm.password2"
              type="text"
              placeholder="Please Enter"
              clearable
            ></el-input>
          </el-form-item>
        </el-form>
        <div class="btns">
          <el-button @click="$emit('changePwdCall')">Cancel</el-button>
          <el-button type="primary" @click="dataFormSubmit()"
            >Confirm</el-button
          >
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { isMobile } from "@/utils/validate";
export default {
  data() {
    var validateMobile = (rule, value, callback) => {
      if (!isMobile(value)) {
        callback(new Error("Phone格式错误"));
      } else {
        callback();
      }
    };
    var password2 = (rule, value, callback) => {
      console.log(value);
      console.log(this.dataForm.password);
      if (value != this.dataForm.password) {
        callback(new Error("两次密码不一样"));
      } else {
        callback();
      }
    };
    return {
      dataForm: {
        password: "",
        password2: ""
      },
      dataRule: {
        password: [
          { required: true, message: "Please Enter", trigger: "blur" }
        ],
        password2: [
          { required: true, message: "Please Enter", trigger: "blur" },
          { validator: password2, trigger: "blur" }
        ]
      }
    };
  },
  props: ["res"],
  mounted() {},

  methods: {
    // Form Submit
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.submit();
        }
      });
    },
    async submit() {
      var res = await this.apis.edit_user({
        id: this.res.id,
        password: this.dataForm.password
      });
      this.$emit("changePwdCall");
      this.$message({
        message: "Operations Success",
        type: "success"
      });
    },

    areaChange(e) {}
  }
};
</script>
<style scoped lang="scss"></style>
